package com.chen.pojo;

import lombok.Data;

@Data
public class CommonResult<T> {
    /**
     * 错误码
     */
    private String code;
    /**
     * 错误信息
     */
    private String msg;
    /**
     * data
     */
    private T data;

    /**
     * 错误格式返回
     * @param code
     * @param msg
     * @return
     */
    public static<T> CommonResult<T> error(String code, String msg) {
        CommonResult<T> result = new CommonResult<>();
        result.setData(null);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    /**
     * 成功的返回
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static<T> CommonResult<T> success(String code, String msg,T data) {
        CommonResult<T> result = new CommonResult<>();
        result.setData(data);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
    public static<T> CommonResult<T> success(String code, String msg) {
        CommonResult<T> result = new CommonResult<>();
        result.setData(null);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
    public static<T> CommonResult<T> success(T data) {
        CommonResult<T> result = new CommonResult<>();
        result.setData(data);
        result.setCode(null);
        result.setMsg(null);
        return result;
    }

}
